package com.gnerv.cms.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 加密工具类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019/5/31
 */
@Slf4j
public class EncryptionUtils {

    public final static String SCRAMBLED_CODE = "lybbendlf";

    private EncryptionUtils(){}

    public static String encryption(String string, String salt){
        String code = scrambledCode(string, salt);
        MessageDigest messageDigest;
        BigInteger sha;
        String res;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(code.getBytes(), 3, 30);
            sha = new BigInteger(messageDigest.digest());
            res = sha.toString(32);
            return res;
        } catch (NoSuchAlgorithmException e) {
            log.info("加密失败！");
        }
        return "";
    }

    public static String encryption(String string){
        return encryption(string, "");
    }

    public static String scrambledCode(String string, String salt){
        return string + salt;
    }

    public static void main(String[] args) {
        String encryption = encryption("123456", "f0e96a28437a4aa0bb8e5245225caa9c");
        System.out.println(encryption);
    }
}
